<?php
/**
 * Description of View
 *
 * @author mateusz.ledzewicz
 */
abstract class View{
    
    public      $basicInformation;
    protected   $content      = null;
    protected   $display      = null;
    protected   $variables;
    protected   $specialVariables;
    
    
    public function __construct()
    {
        $this -> variables           = array();
        $this -> specialVariables    = array();
        $this->basicInformation      = new ViewBasicInformation();
        $this->setVar('scripts', '');
        $this->setVar('head', '');
       
        //Write basic informations 
        //$this->basicInformation -> description = '';
    }
    
    public abstract function load();
    public abstract function showSuccess($message=null);
    protected abstract function chooseView($name);
   
    public function setModel($model, $name=''){
        foreach($model->getColumns() as $col){
            $this->setVar($name.'_'.get_class($model).'_'.$col, $model->$col);
            $this->setVar($name.'_'.get_class($model).'_LABEL_'.$col, $model->labels[$col]);
        }
    }
    
    
    public function setSpecialVar($name, $var){
        $this->specialVariables[$name] = $var;
    }
    
    public function setVar($name, $var)
    {
		$this -> variables[$name] = $var;
	}
	
	protected function getVar($name)
    {
		return $this -> variables[$name];
	}
    
    protected function loadContent($filename){
        $this -> content = file_get_contents(ConfigPath::viewContent.'/'.$filename);
    }
    
    protected function loadText($filename, $parse = array() ){
        $patterns = array();
        $values = array();
        foreach($parse as $key => $value){
            $patterns[] = ConfigSuffix::templateVariable.$key.ConfigSuffix::templateVariable;
            $values[] = $value;
        }
        
        $string = file_get_contents(ConfigPath::viewContent.'/'.$filename);
        $string = str_replace($patterns, $values, $string);
        
        return $string;
    }
    
    protected function loadDisplay($filename){
        $this -> display = file_get_contents(ConfigPath::viewDiplay.'/'.$filename);
    }
    
    protected function loadError($filename){
        return file_get_contents(ConfigPath::viewError.'/'.$filename);
    }
    
    protected function loadHead($filename){
        return file_get_contents(ConfigPath::viewHead.'/'.$filename);
    }
    
    protected function loadSuccess($filename){
        return file_get_contents(ConfigPath::viewSuccess.'/'.$filename);
    }
    
    protected function parse(){
        $patterns = array();
        $values = array();
        foreach($this -> variables as $key => $value){
            $patterns[] = ConfigSuffix::templateVariable.$key.ConfigSuffix::templateVariable;
            $values[] = $value;
        }
        $basicInformation = get_object_vars($this->basicInformation);
        
        foreach($basicInformation as $key => $value){
            $patterns[] = ConfigSuffix::templateVariable.$key.ConfigSuffix::templateVariable;
            $values[] = $value;
        }
        $this->display = str_replace(ConfigSuffix::templateVariable.'content'.ConfigSuffix::templateVariable, $this->content, $this->display);
        //TODO think about limit
        $this->display = str_replace ($patterns, $this->variables, $this -> display);
    }
    
    private function showException($error, $name){
        if(!($error instanceof Error)){
            throw new SystemError(null, 12);
        }
        if($error -> display() !== null) $this->display = $error -> display();
        if($error -> content() !== null) $this->content = $error -> content();
        $this ->setVar($name, $error -> getMessage());
        
    }
    public function showFatalError($error){
        $this->showException($error, 'fatalError');
    }
    public function showSystemError($error){
        $this->showException($error, 'systemError');
    }
    public function showInformationError($error){
        $this->showException($error, 'informationError');
    }
    public function showValidError($error){
        $this->showException($error, 'validError');
    }
    
    public function addScript($script)
    {
        $this->variables['scripts'] .= $script."\n";
    }
    
    public function addHead($text)
    {
        $this->variables['head'] .= $text."\n";
    }
    
    public function display(){
        $this->load(); 
        $this ->parse();
        echo $this->display;
    }
    
    
}

?>
